//////////////////////////////////////////////////////////////////
// MattiseOS Kernel
// Date: 2008
// Author: Matthew Iselin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////

// Includes
#include <mattise.h>
#include <stdint.h>
#include <mem/util.h>
#include <core/intrcommon.h>
#include <core/portio.h>
#include <core/dts.h>

// Variables

// the global TSS
struct tss global_tss;

// the TSS kernel stack (1 page)
uint8_t kernstack[4096];

// the I/O bitmap (ie, the thing that decides which ports can be used)
//uint8_t tss_iobitmap[0xffff] = {0};

// Functions

// sets up the TSS
void kInitTSS()
{
    // zero out the tss
    memset( &global_tss, 0, sizeof( struct tss ) );

    // set it up
    global_tss.ldt = 0; // NO LDT! (we're not hardware multitasking)
    global_tss.ss0 = 0x10;
    global_tss.esp0 = ((uint32_t) kernstack) + 4096;
    global_tss.iomap = (uint32_t) 0; // tss_iobitmap;

    // zero the io bitmap
    //memset( tss_iobitmap, 0, 0xFFFF );

    // add it to the GDT
    kGdtSetGate( 5, (uint32_t) &global_tss, sizeof( struct tss ), 0x89, 0x0F );
}

// loads the TSS
void kLoadTSS()
{
    // load the task register with the TSS entry
    asm volatile( "ltr %%ax" : : "a" (0x28) );
}
